﻿EmployeeInput = Class.create();

EmployeeInput.prototype = Object.extend(new Common(), {
    initialize: function () {
        this.initCommonProperty();

        this.employee_id = "";

        this.callback = null;
    },

    init: function () {

        //保存处理
        Event.observe($(this.dom.btnSave), 'click', this.onClick_Save.bind(this));

        //删除处理
        Event.observe($(this.dom.btnDelete), 'click', this.onClick_Delete.bind(this));

        //清空处理
        Event.observe($(this.dom.btnClear), 'click', this.onClick_Clear.bind(this));

        //关闭处理
        Event.observe($(this.dom.btnClose), 'click', this.onClick_Close.bind(this));

        this.employee_id = $(this.dom.employee_id).value;

        //输入帐号时check是否占用
        Event.observe($(this.dom.employee_code), 'blur', this.onBlur_Check.bind(this));

        //判断是编辑还是添加隐藏删除按钮
        this.initForm();

        //画面加载时权限取得
        //this.getRoleList();

        //画面加载时职位取得
        this.getPositionList();
    },
    //保存処理
    onClick_Save: function () {
        //画面保存时弹出确认框
        if (this.checkData()) {
            CmnMessage.showMessage("是否确定提交处理？", MSG_TYPE_CONFIRM, this.saveData.bind(this));
        }
    },
    //删除处理时弹出确认框
    onClick_Delete: function () {
        CmnMessage.showMessage("确定删除此信息？", MSG_TYPE_CONFIRM, this.deleteData.bind(this));
    },
    //删除处理
    deleteData: function () {
        var data = [];
        data.push(this.employee_id);
        this.sendAjaxByList("/Master/DeleteEmployeeInfo", data, this.onClickDeleteCallback.bind(this));
    },
    //删除CallBack
    onClickDeleteCallback: function (response) {
        var json = this.jsonParse(response);
        if (json.result > 0) {
            CmnMessage.showMessage("删除成功.", MSG_TYPE_INFO, this.messageCallBack.bind(this));
        }
        else {
            CmnMessage.showMessage("删除失败！", MSG_TYPE_ERROR);
        }
    },
    //清空处理
    onClick_Clear: function () {
        this.clearForm(this.dom.clientID);
    },
    //关闭按钮处理
    onClick_Close: function () {
        this.hide();
    },
    //保存员工数据
    saveData: function () {
        this.inputModel = this.getModelByCssClass(this.inputModel, "", this.dom.clientID);
        this.sendAjaxByModel("/Master/SaveEmployeeInfo", this.inputModel, this.saveDataCallback.bind(this));
    },
    //保存后CallBack
    saveDataCallback: function (response) {
        var json = this.jsonParse(response);
        if (json.result > 0) {
            CmnMessage.showMessage(json.message, MSG_TYPE_INFO, this.messageCallBack.bind(this));
        }
        else {
            CmnMessage.showMessage(json.message, MSG_TYPE_ERROR);
        }
    },
    //check帐号是否占用
    onBlur_Check: function () {
        var data = {};
        data.employee_code = $(this.dom.employee_code).value;
        data.employee_id = this.employee_id;
        this.sendAjaxByModel("/Master/CheckEmployeeCd", data, this.onBlurCheckCallback.bind(this));
    },
    //check后CallBack
    onBlurCheckCallback: function (response) {
        var addImg = "";
        $j("#codeMessage").text("");
        if (response == "1") {
            addImg = "<img src='../../Content/images/cahao.jpg' style='width: 11px; height: 11px; padding-left: 5px;' />";
            $j("#codeMessage").css({ "font-size": "11px", "color": "red" });
            $j("#codeMessage").append(addImg);
        }
        else {
            addImg = "<img src='../../Content/images/duihao.jpg' style='width: 15px; height: 15px; padding-left: 5px;' />";
            $j("#codeMessage").css({ "font-size": "11px", "color": "blue" });
            $j("#codeMessage").append(addImg);
        }
    },
    checkData: function () {
        return true;
    },
    //画面加载时权限取得
    getRoleList: function () {
        this.sendAjaxByModel("/Master/GetRoleList", null, this.getRoleListCallback.bind(this));
    },
    //权限CallBack
    getRoleListCallback: function (response) {
        var json = this.jsonParse(response);
        $j("#" + this.dom.role_id).append("<option value=''>--请选择权限--</option>");
        for (var i = 0; i < json.length; i++) {
            $j("#" + this.dom.role_id).append("<option value='" + json[i].role_id + "'>" + json[i].role_name + "</option>");
        }
    },
    //画面加载时职位取得
    getPositionList: function () {
        this.sendAjaxByModel("/Master/GetPositionList", null, this.getPositionListCallback.bind(this));
    },
    //职位CallBack
    getPositionListCallback: function (response) {
        var json = this.jsonParse(response);
        $j("#" + this.dom.position_id).append("<option value=''>--请选择职位--</option>");
        for (var i = 0; i < json.length; i++) {
            $j("#" + this.dom.position_id).append("<option value='" + json[i].position_id + "'>" + json[i].position_name + "</option>");
        }
    },
    //判断是编辑还是添加隐藏删除按钮
    initForm: function () {
        if (this.employee_id == "" || this.employee_id == undefined) {
            $j("#" + this.dom.btnDelete).css("display", "none");
        }
        else {
            $j("#" + this.dom.btnDelete).css("display", "");
        }

        this.clearForm(this.dom.clientID);
        $j("#codeMessage").text("");
    },
    //画面弹出Message框点击确定或取消后处理
    messageCallBack: function () {
        CmnDialog.hide($("addEmployeeInputFrame"));

        if (typeof this.callback == "function") {
            this.callback();
        }
    },
    //点击关闭是隐藏画面
    hide: function () {
        CmnDialog.hide($("addEmployeeInputFrame"));
    },
    //show出画面
    show: function (callback, employee_id) {
        $("addEmployeeInputFrame").style.display = '';
        //show出画面
        CmnDialog.show($("addEmployeeInputFrame"), "新规员工", true);

        this.employee_id = employee_id;

        //初始化页面控件
        this.initForm();
        if (employee_id != undefined && employee_id != null) {
            this.getEmployeeInfo();
        }
        else {
            $(this.dom.employee_id).value = 0;
        }
        this.callback = callback;
    },

    getEmployeeInfo: function () {
        var data = {};
        data.employee_id = this.employee_id;
        this.sendAjaxByModel("/Master/GetEmployeeInfo", data, this.getEmployeeInfoCallback.bind(this));
    },

    getEmployeeInfoCallback: function (response) {
        var json = this.jsonParse(response);
        if (json != null) {
            //把json的值赋给画面
            this.initInputForm(json[0], this.dom.clientID);
        }
    }
});